package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.dao.jpa.BmsBusinessTypeRelationDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessType;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessTypeRelation;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsBusinessTypeRelationService extends BaseService<BmsBusinessTypeRelation, Integer> {

    @Autowired
    BmsBusinessTypeRelationDao bmsBusinessTypeRelationDao;


    @Override
    public BmsBusinessTypeRelationDao getBaseDao() {
        return bmsBusinessTypeRelationDao;
    }

    public void addBusinessRelation(Integer businessId,Integer typeId) {
        BmsBusinessTypeRelation bmsBusinessTypeRelation = bmsBusinessTypeRelationDao.findByBmsBusinessIdAndBmsTypeId(businessId,typeId);
        if(bmsBusinessTypeRelation == null) {
            bmsBusinessTypeRelation = new BmsBusinessTypeRelation();
            bmsBusinessTypeRelation.setBmsBusinessId(businessId);
            bmsBusinessTypeRelation.setBmsTypeId(typeId);
            bmsBusinessTypeRelationDao.save(bmsBusinessTypeRelation);
        }
    }

    public Set<Integer> findAuthBusinessIdInType(Integer typeId) {
        Set<Integer> businessIds = new HashSet<>();
        List<Map<String,Object>> resultList =  bmsBusinessTypeRelationDao.findAuthBusinessIdInType(typeId);
        if(resultList != null && resultList.size() > 0){
            for(Map<String,Object> res : resultList){
                businessIds.add(Integer.parseInt(res.get("id").toString()));
            }
        }
        return businessIds;
    }

    public void postBmsBusinessTypeId(BmsBusiness bmsBusinesses) {
        List<BmsBusiness> businesses = new ArrayList<>();
        businesses.add(bmsBusinesses);
        postBmsBusinessTypeId(businesses);
    }

    public void postBmsBusinessTypeId(List<BmsBusiness> bmsBusinesses) {
        if(bmsBusinesses != null && bmsBusinesses.size() > 0){
            Set<Integer> businessIds = bmsBusinesses.stream().map(BmsBusiness::getId).collect(Collectors.toSet());
            Map<Integer,BmsBusinessTypeRelation> bmsBusinessTypeRelationMap = new LinkedHashMap<>();
            List<BmsBusinessTypeRelation> bmsBusinessTypeRelations = bmsBusinessTypeRelationDao.findByBmsBusinessIdIn(businessIds);
            if(bmsBusinessTypeRelations.size() > 0){
                bmsBusinessTypeRelations.forEach(bt -> {
                    bmsBusinessTypeRelationMap.put(bt.getBmsBusinessId(),bt);
                });
            }
            bmsBusinesses.forEach(bu -> {
                if(bmsBusinessTypeRelationMap.containsKey(bu.getId())){
                    bu.setTagsId(bmsBusinessTypeRelationMap.get(bu.getId()).getBmsTypeId());
                }
            });
        }
    }


}
